<?php
 namespace Admin\Model;
 
 use Zend\Authentication\AuthenticationService;
 use Zend\Authentication\Adapter\DbTable as AuthAdapter;
 use Zend\Authentication\Result;
 
class Login{
    private $auth;
    private $authAdapter;
    
    const NOT_IDENTITY='notIdentity';
    const INVALID_CREDENTIAL='invalidCredential';
    const INVALID_USER='invalidUser';
    const INVALID_LOGIN='invalidLogin';
    
    protected $messages=array(
      self::NOT_IDENTITY=>"No existe la identidad"  ,
      self::INVALID_CREDENTIAL=>"Credencial invalida"  ,
      self::INVALID_LOGIN=>"Login invalido"  ,
      self::INVALID_USER=>"Usuario invalido"  
    );
    
    public function __construct($dbAdapter) {
        $this->authAdapter= new AuthAdapter($dbAdapter,
                'usuario', 'pass');
        $this->auth=new AuthenticationService();
    }
    
    public function login($nick, $password){
        if(!empty($nick) && !empty($password))
        {
            $this->authAdapter->setIdentity($nick);
            $this->authAdapter->setCredential($password);
            $result=  $this->auth->authenticate($this->authAdapter);
            
            switch($result->getCode()){
                case Result::FAILURE_IDENTITY_NOT_FOUND:
                    throw new \Exception($this->_messages[self::NOT_IDENTITY]);
                    break;
                case Result::FAILURE_CREDENTIAL_INVALID:
                    throw new \Exception($this->_messages[self::INVALID_CREDENTIAL]);
                    break;
                case Result::SUCCESS:
                    if($result->isValid()){
                        $data=$this->authAdapter->getResultRowObject();
                        $this->auth->getStorage()->write($data);
                    } else{
                        throw new \Exception($this->_messages[self::INVALID_USER]);
                        
                    }
                    break;
                    
                default:
                    throw new \Exception($this->_messages[self::INVALID_LOGIN]);
                    break;
            
            }
        } else
        {
            throw new \Exception($this->_messages[self::INVALID_LOGIN]);
        }
        return $this;
    }
    
    public  function logout(){
        $this->auth->clearIdentity();
        return $this;
    }
    public function getIdentity(){
        if($this->auth->hasIdentity()){
            return $this->auth->getIdentity();
        }
        return null;
    }
    
    public function isLoggedIn(){
        return $this->auth->hasIdentity();
    }
    public function setMessage($messageString,$messageKey=null){
        if($messageKey===null){
            $keys=array_keys($this->messages);
            $messageKey=current($key);
        }
        if(!isset($this->messages[$messageKey])){
            throw new \Exeption ("no message exist for key '$messageKey'");
            
        }
        $this->messages[$messageKey]=$messageString;
        return $this;
    }
    public function setMessages(array $messages){
        foreach($messages as $key=>$message){
            $this->setMessage($message,$key);
        }
        return $this;
    }
}